package com.jsgt.greekfire.ui.home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.fhx.core.base.BaseAdapter
import com.fhx.core.base.BaseViewHolder
import com.jsgt.greekfire.R
import com.jsgt.greekfire.dao.model.MarketBean
import org.jetbrains.anko.backgroundResource
import org.jetbrains.anko.imageResource
import java.text.DecimalFormat
import java.util.*

/**
 * 行情适配器
 */
class MarketAdapter : BaseAdapter<MarketBean>() {
    private var imageMap: HashMap<String, Int>? = null

    init {
        imageMap = HashMap()
        imageMap!!["btc"] = R.mipmap.ic_coin_btc
        imageMap!!["bch"] = R.mipmap.ic_coin_bch
        imageMap!!["bcv"] = R.mipmap.ic_coin_bcv
        imageMap!!["eos"] = R.mipmap.ic_coin_eos
        imageMap!!["etc"] = R.mipmap.ic_coin_etc
        imageMap!!["eth"] = R.mipmap.ic_coin_eth
        imageMap!!["ltc"] = R.mipmap.ic_coin_ltc
        imageMap!!["trx"] = R.mipmap.ic_coin_trx
        imageMap!!["xrp"] = R.mipmap.ic_coin_xrp
        imageMap!!["dash"] = R.mipmap.ic_coin_dash
        imageMap!!["neo"] = R.mipmap.ic_coin_neo
        imageMap!!["qtum"] = R.mipmap.ic_coin_qtum
        imageMap!!["ada"] = R.mipmap.ic_coin_ada
    }

    var df = DecimalFormat("######0.00")
    override fun provideViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder? {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_market, parent, false)
        return VH(view)
    }

    inner class VH(itemView: View) : BaseViewHolder(itemView) {
        var coin_name_tv: TextView? = null
        var price_dollar_tv: TextView? = null
        var up_and_down_tv: TextView? = null
        var total_24h_tv: TextView? = null
        var rmb_price_tv: TextView? = null
        var imageView: ImageView? = null;

        init {
            coin_name_tv = itemView.findViewById(R.id.coin_name_tv)
            price_dollar_tv = itemView.findViewById(R.id.price_dollar_tv)
            up_and_down_tv = itemView.findViewById(R.id.up_and_down_tv)
            total_24h_tv = itemView.findViewById(R.id.total_24h_tv)
            rmb_price_tv = itemView.findViewById(R.id.rmb_price_tv)
            imageView = itemView.findViewById(R.id.imageView);
        }

        fun bindData(bean: MarketBean) {
            coin_name_tv?.text = bean.symbol!!.split("_")[0].toUpperCase(Locale.getDefault())
            price_dollar_tv?.text = "$${bean.last.toString()}"
            total_24h_tv?.text = "${itemView.context.getString(R.string.market_24_change)}${bean.volume.toString()}"
            rmb_price_tv?.text = "￥${df.format(bean.last?.times(7))}"
            imageView?.imageResource = getImageBySymbol(bean.symbol!!.split("_")[0])!!

            if (bean.changePercentage?.toString()!!.startsWith("-")!!) {
                up_and_down_tv?.text = bean.changePercentage.toString()
                up_and_down_tv?.backgroundResource = R.drawable.bg_market_list_down
            } else {
                up_and_down_tv?.text = bean.changePercentage.toString()
                up_and_down_tv?.backgroundResource = R.drawable.bg_market_list_up
            }

//            var percent = 0.0
//            if(bean.flagPercent != null){
//                percent = bean.flagPercent!!
//            }
//            var percentStr = PriceFormatUtil.formatNumber(percent,2)
//
//            if(TextUtils.equals("0.00",percentStr)){
//                up_and_down_tv?.text = "${percentStr}%"
//                up_and_down_tv?.setBackgroundColor(itemView.resources.getColor(R.color.gray_B9B9B9))
//            }else{
//                if (bean.flag == 0) {
//                    //涨
//                    up_and_down_tv?.text = "+${percentStr}%"
//                    up_and_down_tv?.setBackgroundColor(itemView.resources.getColor(R.color.green_6AB578))
//                } else {
//                    up_and_down_tv?.setBackgroundColor(itemView.resources.getColor(R.color.red_DE6C65))
//                    up_and_down_tv?.text = "-${percentStr}%"
//                }
//            }
        }
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val viewHolder = holder as VH
        viewHolder.bindData(dataList()[position])
    }


    fun getImageBySymbol(symbol: String): Int? {
        return if (imageMap != null && imageMap!!.containsKey(symbol)) {
            imageMap!![symbol]
        } else {
            R.mipmap.ic_coin_btc
        }
    }
}